package com.hp.easyBuy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hp.easyBuy.dao.CommentDao;
import com.hp.easyBuy.entity.DTO.TCommentDTO;
import com.hp.easyBuy.entity.DTO.TCommentDTO2;
import com.hp.easyBuy.entity.TComment;
import com.hp.easyBuy.service.CommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentDao, TComment> implements CommentService {


    @Resource
    private CommentDao commentDao;
    @Override
    public Page<TCommentDTO> doPage(Integer currentpage, Integer pagesize,Long userid) {
        Page<TCommentDTO> page = new Page<TCommentDTO>(currentpage,pagesize);
        QueryWrapper<TCommentDTO> queryWrapper = new QueryWrapper<TCommentDTO>();
        if(userid != null){
            queryWrapper.eq("c.userid", userid);
        }
        Page<TCommentDTO> allCommentPage = commentDao.getAllCommentPage(page,queryWrapper);
        return allCommentPage;
    }

    @Override
    public boolean addComment(TComment comment) {
        comment.setStatus(1);
        comment.setCommenttime(new Date());
        return commentDao.insert(comment) > 0;
    }

    @Override
    public TCommentDTO2 getCommentDTO2Byid(Integer commentid, Long userid) {
        //根据commentid获取对应的数据
        TCommentDTO2 commentDTO2Byid = commentDao.getCommentDTO2Byid(commentid);
        System.out.println("commentdto2的id"+commentDTO2Byid);
        System.out.println("user的id" + userid);
        //查看userid是否和留言对应的id相同，不相同则不返回
        if(commentDTO2Byid == null || commentDTO2Byid.getUserid() != userid){
            return null;
        }else {
            return commentDTO2Byid;
        }
    }
}
